<?php
/**
	ajouter un avis
 **/
//Aseguramos la acci�n - Make sure action is secure
	gatekeeper();
	action_gatekeeper();
	
//Conseguimos los par�metros de entrada - Get input data
	$guid = (int) get_input('guid');
	$rate = (int) get_input('rate');
	$ratecomment= get_input('ratecomment');
//Intentamos conseguir la entidad sobre la que vamos a votar - Make sure we actually have the entity
	if (!$entity = get_entity($guid)){
		register_error(elgg_echo('rate:badguid'));
		forward();
	}

//Comprobamos si la puntuaci�n es correcta, si lo es convertimos la puntuacion a formato [0 - 5]
// Make sure we have a correct rate
	if ($rate && ($rate >= 1 || $rate <= 6)){
		$rate--; 
	}else{
		register_error(elgg_echo('rate:badrate'));
		forward($entity->getUrl());
	}

	
//Si no hemos votado votamos - Let's go rate
	if ($entity->annotate('generic_rate', $rate, $entity->access_id, $_SESSION['guid'])){
		system_message(elgg_echo('rate:saved'));
	}else{
		register_error(elgg_echo('rate:error'));
	}

	// If posting the comment was successful, say so
	if ($entity->annotate('generic_ratecomment',$ratecomment,$entity->access_id, $_SESSION['guid'])) {
		if ($entity->owner_guid != $_SESSION['user']->getGUID())
			notify_user($entity->owner_guid, $_SESSION['user']->getGUID(), elgg_echo('generic_ratecomment:email:subject'), 
				sprintf(
							elgg_echo('generic_ratecomment:email:body'),
							$entity->title,
							$_SESSION['user']->name,
							$ratecomment,
							$entity->getURL(),
							$_SESSION['user']->name,
							$_SESSION['user']->getURL()
						)
			); 
		system_message(elgg_echo("generic_ratecomment:posted"));
	}
	else {
		register_error(elgg_echo("generic_ratecomment:failure"));
	}
	forward("/pg/dashboard/");
?>